<template>
	<el-container>
		<el-header>
			<div class="left-panel">
				<el-button type="primary" icon="el-icon-plus" @click="add"></el-button>
				<el-button type="danger" plain icon="el-icon-delete" :disabled="selection.length==0"
					@click="batch_del"></el-button>
			</div>
			<div class="right-panel">
				<el-input v-model="searchData.storeName" placeholder="设备序列号" clearable style="width:50%">
				</el-input>
				<el-input v-model="searchData.name" placeholder="设备名称" clearable style="width:30%"></el-input>
				<el-input v-model="searchData.tel" placeholder="门店名称" clearable style="width:40%"></el-input>
				<el-select v-model="searchData.status" clearable placeholder="设备状态">
					<el-option v-for="item in statusList" :key="item.id" :label="item.label"
						:value="item.value" />
				</el-select>
				<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
			</div>
		</el-header>
		<el-main class="nopadding">
			<scTable ref="table" :data="list.apiObj" row-key="id" @selection-change="selectionChange" stripe
				:hideDo="true">
				<el-table-column type="selection" width="50"></el-table-column>
				<el-table-column label="门店名称" prop="custName" width="220">
				</el-table-column>
				<el-table-column label="设备状态" prop="custNameQqa" width="100" align="center">
					<template #default="scope">
						<sc-status-indicator v-if="scope.row.state" type="success"></sc-status-indicator>
						<sc-status-indicator v-if="!scope.row.state" pulse type="danger"></sc-status-indicator>
					</template>
				</el-table-column>
				<el-table-column label="设备序列号" prop="sn"></el-table-column>
				<el-table-column label="设备名称" prop="deviceName" width="250"></el-table-column>
				<el-table-column label="设备唯一识别码" prop="snId"></el-table-column>
				<el-table-column label="安装时间" prop="create_time" width="150"></el-table-column>
				<el-table-column label="操作" fixed="right" align="right">
					<template #default="scope">

						<el-button type="primary" plain size="small" @click="table_edit(scope.row)">编辑</el-button>
						<el-button v-role="['SAQ','admin']" plain size="small"
							@click="table_manager(scope.row)">解绑</el-button>
						<!-- <el-button plain size="small" @click="table_managerAdmin(scope.row)">店员审核</el-button>
						<el-button plain size="small" @click="table_device(scope.row)">设备</el-button> -->
						<el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
							<template #reference>
								<el-button plain type="danger" size="small">删除</el-button>
							</template>
						</el-popconfirm>
					</template>
				</el-table-column>
			</scTable>
		</el-main>
	</el-container>

	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSaveSuccess"
		@closed="dialog.save=false"></save-dialog>
	<qrcodeDialog v-if="dialog.qrcode" ref="qrcodeDialog" @success="handleSaveSuccess" @closed="dialog.qrcode=false">
	</qrcodeDialog>
	<deviceDialog v-if="dialog.device" ref="deviceDialog" @success="handleSaveSuccess" @closed="dialog.device=false">
	</deviceDialog>
	<el-drawer v-model="dialog.info" :size="800" title="详细" direction="rtl" destroy-on-close>
		<info ref="infoDialog"></info>
	</el-drawer>
	<el-drawer v-model="dialog.manager" :size="800" title="选择店长" direction="rtl" destroy-on-close>
		<manager ref="managerDialog"></manager>
	</el-drawer>
	<el-drawer v-model="dialog.managerAdmin" :size="800" title="店员审核" direction="rtl" destroy-on-close>
		<managerAdmin ref="managerAdminDialog"></managerAdmin>
	</el-drawer>
</template>

<script>
	import saveDialog from './save'
	import info from './info'
	import manager from './manager'
	import managerAdmin from './managerAdmin'
	import qrcodeDialog from './qrcode'
	import deviceDialog from './device'
	export default {
		name: 'store',
		components: {
			saveDialog,
			info,
			manager,
			qrcodeDialog,
			deviceDialog,
			managerAdmin
		},
		data() {
			return {
				dialog: {
					save: false,
					info: false,
					qrcode: false,
					device: false,
					manager: false,
					managerAdmin: false,
				},
				searchData: {
					storeName: '',
					name: '',
					tel: '',
					status:''
				},
				statusList:[
					{
						label:'在线',
						value:1
					},
					{
						label:'离线',
						value:0
					}
				],
				list: {
					apiObj: [{
							"id": "410000198611212735",
							"custName": "椰海店",
							deviceName: '摄像头',
							"sn": "2198061243WLLB003520",
							"snId": "70001143237625444958208",
							"custNameQqa": "郑总",
							"custPhoneQqa": "19989765666",
							"shopCode": '4397',
							storeManager: '张部',
							create_time: '2023-10-12 10:12:14',
							state: false
						},
						{
							"id": "410000198611212735",
							"custName": "东方店",
							deviceName: '摄像头',
							"sn": "2198061245WLL8000273",
							"snId": "70001123615517911941120",
							"custPhoneQqa": "19989765666",
							"shopCode": '4397',
							create_time: '2023-10-12 10:12:14',
							state: true
						},
					]
					//this.$API.demo.list
				},
				selection: []
			}
		},
		mounted() {

		},
		methods: {
			//搜索
			upsearch() {
				//this.$refs.table.upData(this.search)
			},
			//窗口新增
			add() {
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open()
				})
			},
			//窗口编辑
			table_edit(row) {
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open('edit').setData(row)
				})
			},
			//窗口编辑
			table_qrcode(row) {
				this.dialog.qrcode = true
				this.$nextTick(() => {
					this.$refs.qrcodeDialog.open().setData(row)
				})
			},
			table_device(row) {
				this.dialog.device = true
				this.$nextTick(() => {
					this.$refs.deviceDialog.open().setData(row)
				})
			},
			//页面新增
			addPage() {
				this.$router.push({
					path: '/template/list/crud/detail',
				})
			},
			//页面编辑
			table_edit_page(row) {
				this.$router.push({
					path: '/template/list/crud/detail',
					query: {
						id: row.id
					}
				})
			},
			//查看
			table_show(row) {
				this.dialog.info = true
				this.$nextTick(() => {
					this.$refs.infoDialog.setData(row)
				})
			},
			table_manager(row) {
				this.dialog.manager = true
				this.$nextTick(() => {
					this.$refs.managerDialog.setData(row)
				})
			},
			table_managerAdmin(row) {
				this.dialog.managerAdmin = true
				this.$nextTick(() => {
					this.$refs.managerAdminDialog.setData(row)
				})
			},
			//删除明细
			async table_del(row, index) {
				var reqData = {
					id: row.id
				}
				var res = await this.$API.demo.post.post(reqData);
				if (res.code == 200) {
					this.$refs.table.removeIndex(index)
					this.$message.success("删除成功")
				} else {
					this.$alert(res.message, "提示", {
						type: 'error'
					})
				}
			},
			//批量删除
			async batch_del() {
				var confirmRes = await this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
					type: 'warning',
					confirmButtonText: '删除',
					confirmButtonClass: 'el-button--danger'
				}).catch(() => {})

				if (!confirmRes) {
					return false
				}

				var ids = this.selection.map(v => v.id)
				this.$refs.table.removeKeys(ids)
				this.$message.success("操作成功")

			},
			//表格选择后回调事件
			selectionChange(selection) {
				this.selection = selection
			},
			//本地更新数据
			handleSaveSuccess(data, mode) {
				//为了减少网络请求，直接变更表格内存数据
				if (mode == 'add') {
					this.$refs.table.unshiftRow(data)
				} else if (mode == 'edit') {
					this.$refs.table.updateKey(data)
				}

				//当然也可以暴力的直接刷新表格
				// this.$refs.table.refresh()
			}
		}
	}
</script>

<style>
</style>
